$(function () {

//layui表格
    layui.use(['table','form','layer'], function () {
    	var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var T = table.render({
            elem: '#ClassTable'
            
            , url: '/ClassSet' //数据接口
            ,response : {
                statusCode : 200
                // 重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData : function(res) { // res 即为原始返回的数据
                return {
                    "count" : res.tol,
                    "code" : res.status, // 解析接口状态
                    "msg" : res.message, // 解析提示文本
                    "data" : res.classes
                    // 解析数据列表
                };
            }
            , cols: [[ //表头
                {field: 'classId', title: '班级编号',align:'center', width: 200, sort: true, fixed: 'left'}
                , {field: 'deptName',align:'center', title: '院系名称'}
                , {field: 'className',align:'center', title: '班级名称'}                
                , {field: 'operate',align:'center',toolbar : '#barDemo', title: '操作', width: 180}
            ]]
        });




        //监听删除按钮
        table.on('tool(ClassTable)', function (obj) {
            var data = obj.data;
            if (obj.event == 'del') {

                layer.confirm('真的删除行吗', function (index) {
                    $.ajax({
                        url: "/deleteClass",
                        type: "post",
                        data: {"classId": data.classId},
                        dataType: "json",
                        success: function () {
                            obj.del();
                            layer.close(index);
                            layer.msg("删除成功");
                        },
                        error: function () {
                            layer.msg("error");
                            layer.close(index);
                        }
                    })

                });
            }
            /*else if(obj.event == "edit"){          //监听编辑按钮
            	 var data = obj.data;			      	   
					layer.open({
						title:'班级信息',
						type : 1,
						skin : 'layui-layer-rim', // 加上边框
						area : [ '520px',
								'400px' ], // 宽高
						content : '<form class="layui-form layui-form-pane" action="#" id="form1">\n'
								+ '  <br>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">班级号</label>\n'
								+ '    <div class="layui-input-block" >\n'
								+ '      <input type="text" id="input1" name="classId" lay-verify="title" autocomplete="off" readonly disabled class="layui-input form-control" >\n'
								+ '    </div>\n'
								+ '  </div>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">班级名</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text" id="input2" name="className" lay-verify="title" autocomplete="off"  disabled readonly class="layui-input form-control">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">院系</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <select id="select1" name="deptName" lay-filter="select1" lay-verify="">\n'
								+ '        <option></option>\n'
								+ '      </select>\n'
								+ '    </div>\n'
								+ '  </div>'
								+ '<div><button type="button" id="form1_btn" class="layui-btn" style="margin-left: 160px;">提交</button></div>\n'	
								
								+ '</form>'
					});
                    var classId = data.classId;
                    var className = data.className;

						// 添加数据到表
						$("#input1").val(classId);// 班级ID
						$("#input2").val(className);// 班级姓名
						
						$.ajax({
							url : '/teacher_dept',
							dataType : 'json',
							type : 'post',
							success : function(data) {
								var d = eval(data);
								console.log(d);
								for (var i = 0; i < d.length; i++) {
									$("#select1").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
									console.log("......"+d[i].deptName+".."+d[i].deptId);
									form.render('select');
								}
							}
						})
						
						//提交
						 form.on('submit(btn1)', function(data){
							var formData = data.field;
							var deptId = formData.deptName;
							
							var deptName = $("#select1 option:selected").text();
							
							var classId = $("#input1").val();

							if(deptName !=null && deptName != '' ){
								
								$.ajax({
								url:'/updateClass',
								dataType:'json',
								type:'post',
								data:{
									"classId":classId,
									"deptId":deptId		
								},
								success:function(r){
									if(r.msg =="YES"){
										layer.closeAll();
										T.reload();
									}
									layer.msg("信息修改成功");
								},
								error:function(){
									layer.alert('修改失败',{
										icon : 2,
										skin : 'layer-ext-moon'
									})
								}
							})
						}else{
							layer.alert('请将信息填写完整', {
								icon: 0,
								skin: 'layer-ext-moon' 
								})
						}
					})	
            	
            }*/
        });


    

    //添加班级
	    $("#Set_class").on("click", function () {
	        layer.open({
	            type: 1,
	            skin: 'layui-layer-rim', //加上边框
	            area: ['520px', '300px'], //宽高
	            content: '<form class="layui-form layui-form-pane" action="#">\n' +
	                '  <div class="layui-form-item">\n' +
	                '    <label class="layui-form-label" >班级号</label>\n' +
	                '    <div class="layui-input-block">\n' +
	                '      <input type="text" name="classId" lay-verify="title" autocomplete="off" placeholder="请输入班级编号" class="layui-input"  >\n' +
	                '    </div>\n' +
	                '  </div>\n' +
	                '  <div class="layui-form-item">\n' +
	                '    <label class="layui-form-label" >院系</label>\n' +
					'    <div class="layui-input-block">\n'  +
					'      <select id="select1" name="deptName" lay-filter="select1" lay-verify="">\n'+
					'        <option></option>\n'+
					'      </select>\n'+
					'    </div>\n'+
	                '  </div>\n' +
	                '  <div class="layui-form-item">\n' +
	                '    <label class="layui-form-label" >班级名</label>\n' +
	                '    <div class="layui-input-block">\n' +
	                '      <input type="text" name="className" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n' +
	                '    </div>\n' +
	                '  </div>\n' +
	                '<div ><button type="button" lay-submit lay-filter="btn1" class="layui-btn" style="margin-left: 160px;" id="button1">提交</button>\n' +
	                '<button type="reset" class="layui-btn layui-btn-primary" id="button2">重置</button></div>\n' +
	                '</form>'
	        });
	        
			$.ajax({
				url : '/teacher_dept',
				dataType : 'json',
				type : 'post',
				success : function(data) {
					var d = eval(data);
					console.log(d);
					for (var i = 0; i < d.length; i++) {
						$("#select1").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
						console.log("......"+d[i].deptName+".."+d[i].deptId);
						form.render('select');
					}
				}
			});
        
			
			form.on('submit(btn1)', function(data){
				var formData = data.field;
				var deptId = formData.deptName;
				var classId = formData.classId;
				var className = formData.className;
				
	    			 
	    			 if(classId == "" || className == "" || deptId == ""){
	    				 layer.alert("数据不能为空")
	    			 }else{
				    		$.ajax({
				    			type:"post",
				    			url:"/insertClass",
				    			datatype : 'json',
				    			data : {
				    				"classId" : classId,
				    				"className" : className,
				    				"deptId" : deptId
				    			},
				    			success : function(data) {
				                	 if(data.msg == "班级号被占有"){
				                		 layer.alert("班级号已有");		                					                		 
				                	 }else if(data.msg == "成功"){
				                		
											layer.closeAll();
											T.reload();
											layer.msg("添加成功")
				                	 }else if(data.msg=="班级名已存在"){
				                		 layer.alert("班级名已有");	
				                	 }
				    			}
				    		});
	    			 }
	    			 return false;
	    		 })

	    		 
		      
	    	 
    	})
    	
    	//显示院系按钮
    	$.ajax({
    		url:'/getDepartment',
    		dataType:'json',
    		type:'post',
    		success:function(res){
    			var da = eval(res);
				console.log(da);

				for (var i = 0; i < da.length; i++) {
					$("#department_select").append(
							"<button type=\"button\" class=\"btn btn-default\">"
									+ da[i].deptName
									+ "</button>");
				}
    		},error:function(){
    			layer.msg("院系信息error");
    		}
    	})
    	
    	//点击院系按钮
    	$("#department_select").on("click","button",function(){
    		var deptName = $(this).text();
    		console.log(deptName);
			$("#span").html(deptName);
			if(deptName == "全部院系"){
				table.reload('ClassTable',{
					url:'/ClassSet'
				})
			}else{
				table.reload('ClassTable',{
					url : '/getClassList',
					where : {
						"deptName" : deptName
					}
				})
			}
    	})
    	
    });
})